<template>
  <div class="showcase">
    <!-- 标题部分 -->
    <div
      class="showcase-title"
      @click="foldFunc"
      :class="{ isFoldClass: isFold }"
    >
      <span>{{ title }} ♪(^∇^*)</span>
      <el-icon class="title-icon" :size="20" color="#02a13f">
        <arrow-down :class="{ foldFlag: isFold }" />
      </el-icon>
    </div>
    <!-- 内容部分 -->
    <el-row :gutter="3" :class="{ foldCase: isFold }">
      <el-col
        v-for="item in caseList"
        :key="item.id"
        :span="2"
        :offset="2"
        :xs="9"
        :sm="6"
        :md="4"
        :lg="3"
        :xl="3"
      >
        <el-card
          class="showcase-item"
          :body-style="{ padding: '6px' }"
          shadow="always"
          @click="caseItemClick(item)"
        >
          <!-- 多个数据存储的图片name不一样 -->
          <img
            :src="
              item.picUrl
                ? item.picUrl
                : item.picWebUrl
                ? item.picWebUrl
                : item.al.picUrl
            "
            class="image"
          />
          <div style="padding: 2px" class="info">
            <span class="name">{{ item.name }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import emitter from '@/utils/mitt/eventbus' /* 事件总线 */
import localCache from '@/utils/cache' /* 本地缓存 */

export default defineComponent({
  props: {
    /* 传入要展示的内容 */
    name: String,
    title: String,
    caseList: Array,
    isSong: Boolean
  },
  setup(props) {
    const store = useStore()
    const router = useRouter()
    let isFold = ref(false) /* 橱窗是否折叠 */
    const foldFunc = () => {
      isFold.value = !isFold.value
    }
    const startPlay = ref(false) /* 是否开始播放 */
    const playCacheList = ref<any>([]) /* 播放列表具体数据 */
    const nameCacheList = ref<any>([]) /*  播放列表名字数据，便于播放列表展示*/
    const lyricCacheList = ref<any>(
      []
    ) /* 播放列表内的歌词数据，便于音乐台切换歌词 */
    const count = ref(0) /* 记录歌曲数量 */
    const playList = ref({
      count,
      playCacheList,
      nameCacheList,
      lyricCacheList
    }) /* 播放列表内音乐详情数据 */
    const caseItemClick = async (item: any) => {
      if (props.isSong) {
        /* 歌曲 执行播放逻辑 */
        /* 请求歌曲详情数据，内有播放用的url */
        await store.dispatch({
          type: 'song/getSongDetailAction',
          id: item.id
        })
        /* 请求歌曲信息数据，内有展示用的名字 */
        await store.dispatch({
          type: 'song/getSongInfoAction',
          id: item.id
        })
        /* 请求歌词 */
        await store.dispatch({
          type: 'song/getSongLyricAction',
          id: item.id
        })
        /* 取出数据 */
        const songDetail = store.state.song.songDetail[0]
        const lyric = store.state.song.lyric
        /* 开始执行播放逻辑 */
        startPlay.value = true
        /* 分发事件，携带播放用的url */
        emitter.emit('getSongUrl', songDetail.url)
        /* 歌曲数量+1 */
        playList.value.count++
        /* 播放列表首位添加当前歌曲 */
        playCacheList.value.unshift(songDetail.url)
        nameCacheList.value.unshift(item.name)
        lyricCacheList.value.unshift(lyric)
        /* 存储到localstroage */
        localCache.setCache('currentName', item.name)
        localCache.setCache('currentUrl', songDetail.url)
        localCache.setCache('playList', playList.value)
        localCache.setCache('currentLyric', lyric)
        /* 分发事件，携带当前歌曲在播放列表中的位置 */
        emitter.emit('isPlaying', 0)
      } else {
        /* 非歌曲，路由跳转 */
        router.push({
          path: '/main/sheetdetail',
          query: {
            id: item.id
          }
        })
      }
    }
    return {
      isFold,
      foldFunc,
      caseItemClick
    }
  }
})
</script>

<style scoped lang="less">
.showcase {
  .isFoldClass {
    background: rgba(110, 255, 224, 0.329);

    .foldFlag {
      transition: all 0.6s ease;
      transform: translateY(50px);
      transform: rotateZ(90deg);
      transform-origin: 10 10;
    }
  }

  .foldCase {
    height: 0;
    overflow: hidden;
  }
  .showcase-title {
    height: 30px;
    margin: 0 0.2rem 0.16rem 0.2rem;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-between;
    align-items: center;
    font-size: 16px;
    color: rgb(2, 161, 63);
    font-weight: 600;
    font-style: italic;
    line-height: 30px;
    text-indent: 20px;
    cursor: pointer;
    text-align: right;
    border-bottom: 1px solid #ccc;
    position: relative;

    &::before {
      flex: 1;
      content: '    / / /';
      display: inline-block;
      width: 90%;
      border-bottom: 1px solid #ccc;
      font-size: 12px;
      color: rgb(102, 102, 102);
    }
    span {
      flex: 3;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .title-icon {
      position: absolute;
      left: 0;
    }
  }
  .showcase-item {
    margin-bottom: 20px;
    border-radius: 0 0 16px 16px;
    cursor: pointer;

    &:hover {
      transform: scale(1.05);
    }

    .image {
      width: 100%;
      height: 2rem;
      display: block;
      border-radius: 0 0 6px 6px;
    }
    .info {
      height: 30px;
      line-height: 30px;
      .name {
        text-indent: 3px;
        display: inline-block;
        width: 100%;
        font-size: 13px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
}
</style>
